﻿using Inovout.Kuajing.Models;
using Inovout.Kuajing.Store.Web.Models;
using Inovout.Locations;
using Inovout.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Store.Web.Controllers
{
    public class LogisticsController : KJBaseController<ShipmentOrder>
    {
        private ILocationService locationService;
        public LogisticsController(ILocationService locationService) 
        {
            this.locationService = locationService;
        }

        public ActionResult CreateShippingTemplate()
        {
            Location location = locationService.FindById(0);//0: 国家
            List<Location> locationList = locationService.FindByParent(location).ToList();

            var data = from c in locationList
                       select new { GoodCountry = c.Id, GoodCountryName = c.Name };

            ViewData["Countries"] = new SelectList(data, "GoodCountry", "GoodCountryName");

            IList<Object> listTime = new List<Object>();
            listTime.Add(new { DeliveryTime = "1", DeliveryTimeName = "4天之内" });
            listTime.Add(new { DeliveryTime = "2", DeliveryTimeName = "5天之内" });

            ViewData["DeliveryTime"] = new SelectList(listTime, "DeliveryTime", "DeliveryTimeName");
            return View();
        }

        [HttpPost]
        public ActionResult SaveShippingTemplate(ShippingTemplate shippingTemplate, string freight, string freeship)
        {
            IRepositoryService<ShippingTemplate> shippingTemplateService = ComponentRegistry.Resolve<IRepositoryService<ShippingTemplate>>();
            shippingTemplateService.Save(shippingTemplate);

            List<FreightInfo> freightInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FreightInfo>>(freight);
            List<FreeShippingInfo> freeshipInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FreeShippingInfo>>(freeship);
            
            IRepositoryService<ShippingExpression> shippingExpressionService = ComponentRegistry.Resolve<IRepositoryService<ShippingExpression>>();
            IRepositoryService<FreeShippingExpression> freeShippingExpressionService = ComponentRegistry.Resolve<IRepositoryService<FreeShippingExpression>>();

            foreach (FreightInfo freightInfo in freightInfoList) {
                ShippingExpression shippingExpression = new ShippingExpression();
                shippingExpression.FirstPiece = freightInfo.FirstInMeasure;
                shippingExpression.FirstCharge = freightInfo.FirstMoney;
                shippingExpression.RenewPiece = freightInfo.RenewMeasure;
                shippingExpression.RenewCharge = freightInfo.RenewMoney;
                shippingExpression.ShippingWay = (ShippingWay)freightInfo.ShippingWay;
                shippingExpression.ShippingTemplate = shippingTemplate;
                shippingExpressionService.Save(shippingExpression);
            }

            foreach (FreeShippingInfo freeShippingInfo in freeshipInfoList) {
                FreeShippingExpression freeShippingExpression = new FreeShippingExpression();
                freeShippingExpression.ShippingTemplate = shippingTemplate;
                freeShippingExpression.PriceWay = (PriceWay)freeShippingInfo.PriceWay;
                freeShippingExpression.ShippingWay = (ShippingWay)freeShippingInfo.ShippingWay;
                freeShippingExpression.Quality = freeShippingInfo.Quality;
                freeShippingExpression.Money = freeShippingInfo.Money;
                freeShippingExpressionService.Save(freeShippingExpression);
            }
            return RedirectToAction("UnShippedOrderList","Order");
        }
    }
}